<?php

class links
{
   var $DB_obj  = null;
   var $meccano = null;
   var $params  = array();
   var $error   = false;

   var $version = '0.2.2';
   var $serial  = '20070411';

   function links(&$DB, &$MECCANO)
   {
      if(is_object($DB))
      {
         $this->DB_obj = &$DB;
      } else
      {
         $this->error['system'][] = "No DateBase Object registered!";
      }

      if(is_object($MECCANO))
      {
         $this->meccano = &$MECCANO;
      } else
      {
         $this->error['system'][] = "No MECCANO Object registered!";
      }

      if(isset($this->meccano->system['allow_tree']))
      {
         $this->params['tree'] = isBool($this->meccano->system['allow_tree']);
      } else
      {
         $this->params['tree'] = false;
      }
      if(isset($this->meccano->system['link_content']))
      {
         $this->params['content'] = isBool($this->meccano->system['link_content']);
      } else
      {
         $this->params['content'] = false;
      }

      $this->SetParams();

      $this->error = false;
   }

   /**
      * Get all entries
      *
      * @param integer $category
      * @param string $mode
      * @return array|false
      */
   function Get($category = false, $mode = '')
   {
      $modes     = array('a','t','c','r');
      // OUTPUT ORDERS: a - as intput, t - by title, c - by category, r - random
      $sort      = ' ASC';
      $order     = '';
      $condition = '';

      if(strlen($mode) > 1)
      {
         if(substr($mode, 1, 1) == '-') $sort = ' DESC';
      }
      $mode  = substr($mode, 0, 1);
      $mode  = in_array($mode, $modes) ? $mode : 'a';
      $order = " ORDER BY tb.id" . $sort;

      switch ($mode)
      {
         case 't':
            // Order by Title
            $order = " ORDER BY BINARY tb.title" . $sort;
            break;

         case 'c':
            // Order by Category
            if($this->params['tree'] != false)
            {
               $order = ' GROUP BY tb.cat' . $sort;
            }
            break;

         case 'r':
            // Random order
            $order = " ORDER BY RAND()";
            break;
      }

      if($this->params['tree'] != false)
      {
         isDigit($category);
         if($category == false)
         {
            $condition[] = "tree.module = 'links'";
         } else
         {
            $categories  = $this->meccano->ListOfID($category);
            $categories  = implode(',',$categories);
            $condition[] = "tb.cat IN (" . $categories . ")";
         }
      }

      if($this->params['language'] != false)
      {
         if($this->params['tree'] == true)
         {
            $condition[] = "trans.lang = '" . $this->params['language'] . "'";
         }
         $condition[] = "tb.lang = '" . $this->params['language'] . "'";
      }
      if($this->params['tree'] == true)
      {
         $condition[] = "tb.lang = trans.lang";
      }

      if(is_array($condition))
      {
         $condition = implode(" AND ", $condition) . " ";
      }

      if($this->params['tree'] == true)
      {
         $this->DB_obj->sql = "SELECT tb.*, trans.title AS category FROM meccano_links AS tb, meccano_tree AS tree, meccano_treetranslates AS trans WHERE tb.cat = tree.id AND tb.cat = trans.id AND " . $condition . $order;
      } else
      {
         if($condition != false) $condition = " WHERE " . $condition;
         $this->DB_obj->sql = "SELECT * FROM meccano_links AS tb ".$condition.$order.";";
      }

      return $this->DB_obj->DB_Fetch();
   }

   function View($id)
   {
      isDigit($id);
      if($id == false) return false;

      $this->DB_obj->sql = "SELECT * FROM meccano_links WHERE id = ".$id.";";
      $this->DB_obj->DB_Fetch();

      if($this->DB_obj->rows > 0)
      {
         $link = $this->DB_obj->fetched;
         if($this->params['tree'])
         {
            $category = $this->meccano->GetNode($link[0]['cat'], $link[0]['lang']);
            $link[0]['category'] = $category['title'];
            $link[0]['node'] = &$category;
         }

         return $link;
      }

      return false;
   }

   function Add($vars)
   {
      if(!is_array($vars)) $this->error[] = "There is no datas handled";
      if($this->params['tree'] == true && (!isset($vars['cat']) || $vars['cat'] == false)) $this->error[] = "No Category selected";
      if(!isset($vars['title']) || $vars['title'] == false) $this->error[] = "No Title inputed";
      if($this->error !== false) return false;

      $this->DB_obj->DB_QueryBuilder("INSERT INTO meccano_links (cat, title, link, email, description, lang) VALUES (%i, '%s', '%s', '%s', '%s', '%s')", array($vars['cat'], $vars['title'], $vars['link'], $vars['email'], $vars['description'], $vars['lang']));

      return $this->DB_obj->DB_Query();
   }

   /**
      * UPDATE link
      *
      * @param integer $id
      * @param array $vars
      * @return bool
      */
   function Update($id, $vars)
   {
      isDigit($id);
      if($id == false) return false;

      if(!is_array($vars)) $this->error[] = "There is no datas handled";
      if(!isset($vars['title']) || $vars['title'] == false) $this->error[] = "No Title inputed";
      if(!isset($vars['cat']) || $this->params['tree'] == false)
      {
         $vars['cat'] = null;
      } else
      {
         isDigit($vars['cat']);
         if($vars['cat'] == false) $this->error[] = "No Category selected";
      }
      if($this->error != false) return false;

      $this->DB_obj->DB_QueryBuilder("UPDATE meccano_links SET cat=%i, title='%s', link='%s', email='%s', description='%s', lang='%s' WHERE id=%i", array($vars['cat'], $vars['title'], $vars['link'], $vars['email'], $vars['description'], $vars['lang'], $id));

      return $this->DB_obj->DB_Query();
   }

   function Delete($id)
   {
      $del = array();
      if(is_array($id))
      {
         $del = array_filter($id, 'isDigit');
      } else
      {
         isDigit($id);
         if($id != false) $del[] = $id;
      }
      unset($id);

      if(count($del) > 0)
      {
         $del = implode(',', $del);
         $this->DB_obj->sql = "DELETE FROM meccano_links WHERE id IN (".$del.");";

         return $this->DB_obj->DB_Query();
      }

      return false;
   }

   function SetParams($params = 'clear')
   {
      if($params == 'clear')
      {
         $this->params['language'] = false;
         return true;
      }

      $params = explode(';', $params);

      foreach ($params as $value)
      {
         $p = explode('=', $value);
         switch ($p[0])
         {
            case 'language':
               strtolower($p[1]);
               if(in_array($p[1], $this->meccano->system['lang'])) $this->params['language'] = $p[1];
               break;
         }
      }
   }

   var $build = array(
   'links'=>
   "CREATE TABLE meccano_links (
    id int(10) unsigned auto_increment,
    title varchar(255) NOT NULL,
    link varchar(255) NOT NULL,
    email varchar(255),
    description text,
    paged enum('0','1') default '0',
    lang char(2) default 'ru',
    PRIMARY KEY(id),
    UNIQUE KEY(link));"
    );
}
?>